📁 手写 您所在的位置:网站首页 vue 虚拟键盘 📁 手写

📁 手写

2023-04-06 07:32| 来源: 网络整理| 查看: 265

call

Function.prototype.myCall = function (obj, ...arg) { const context = obj || window; const fn = Symbol(); // 保证唯一 context[fn] = this; const res = context[fn](...arg); delete context[fn]; return res;}

apply

Function.prototype.myApply = function (obj, arg) { const context = obj || window; const fn = Symbol(); // 保证唯一 context[fn] = this; const res = context[fn](...arg); delete context[fn]; return res;}

bind ```javascript Function.prototype.bind = function (obj, …args) { const context = obj || window; const fn = Symbol() context[fn] = this; let that = this;

const res = function(…others){

return that.apply( this instanceof that ? this : context, //new时, this指向res的实例,res继承自that [...args,...others])

}

// 如果绑定的是构造函数 那么需要继承构造函数原型属性和方法 res.prototype = Object.create(that.prototype)

return res }

// 使用 function Point(x, y) { this.x = x; this.y = y; }

Point.prototype.toString = function () { return this.x + ‘,’ + this.y; }

let YPoint = Point.bind(null, 1); let axiosPoint = new YPoint(2);

console.log(axiosPoint.toString()) // ‘1,2’ ```



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有